//
//  MineHeaderView.m
//  ScalperMerchant
//
//  Created by 王云 on 2017/9/30.
//  Copyright © 2017年 WY. All rights reserved.
//

#import "MineHeaderView.h"

@interface MineButton : UIButton

@property (nonatomic,weak)UILabel *numL;
@property (nonatomic,weak)UILabel *titleL;

@end
@implementation MineButton

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        
        UILabel *numL = [[UILabel alloc] init];
        numL.text = @"0";
        [numL setTextAlignment:NSTextAlignmentCenter];
        numL.font = [UIFont systemFontOfSize:14];
        numL.textColor = WYColor(229, 61, 66);
        [self addSubview:numL];
        _numL = numL;
        
        UILabel *titleL = [[UILabel alloc] init];
        [titleL setTextAlignment:NSTextAlignmentCenter];
        titleL.font = [UIFont boldSystemFontOfSize:12];
        [self addSubview:titleL];
        _titleL = titleL;
        
        UIImageView *lineI = [[UIImageView alloc] init];
        lineI.backgroundColor = DarkColor;
        [self addSubview:lineI];
        
        __weak __typeof(&*self)weakSelf = self;
        [numL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.with.mas_equalTo(weakSelf);
            make.bottom.equalTo(weakSelf.mas_centerY).offset(-4);
        }];
        [titleL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.with.mas_equalTo(weakSelf);
            make.top.equalTo(numL.mas_bottom);
            make.height.mas_equalTo(weakSelf.frame.size.height/2);
        }];
        [lineI mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(weakSelf);
            make.right.equalTo(weakSelf.mas_right).offset(1);
            make.height.mas_equalTo(weakSelf.frame.size.height-20);
            make.width.mas_equalTo(@1);
        }];
    }
    return self;
}


@end

@interface MineHeaderView ()

@property (nonatomic,weak)UIImageView *avatorImg;
@property (nonatomic,weak)UILabel *shopNameL;
@property (nonatomic,weak)UILabel *nameL;
@property (nonatomic,weak)UILabel *shopType;

@property (nonatomic,weak)MineButton *mineLBtn;
@property (nonatomic,weak)MineButton *mineCBtn;
@property (nonatomic,weak)MineButton *mineRBtn;

@property (nonatomic,strong)SCUser *user;

@end

@implementation MineHeaderView

- (instancetype)initWithFrame:(CGRect)frame option:(Option)option{
    MineHeaderView *headerV = [[MineHeaderView alloc] initWithFrame:frame];
    headerV.option = option;
    return headerV;
}
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
     
        [self setUI];
    }
    return self;
}
- (void)setUI{
    
    UIImageView *imgV = [[UIImageView alloc] init];
    imgV.image = [UIImage imageNamed:@"backgeound"];
    [self addSubview:imgV];
    
    UIImageView *avatorImg = [HWCommon hw_roundedRectWith:35 rect:CGRectMake(0, 0, 60, 60) byRoundingCorners:UIRectCornerAllCorners];
    avatorImg.image = [UIImage imageNamed:@"logo"];
    [imgV addSubview:avatorImg];
    
    UILabel *shopNameL = [[UILabel alloc] init];
//    shopNameL.text = @"店面 : 简朴寨";
    shopNameL.text = self.user.member_name;
    shopNameL.textColor = [UIColor whiteColor];
    shopNameL.font = [UIFont boldSystemFontOfSize:14];
    [imgV addSubview:shopNameL];
    _shopNameL = shopNameL;
    
//    UILabel *nameL = [[UILabel alloc] init];
//    nameL.text = @"昵称 : 小黑";
//    nameL.textColor = [UIColor whiteColor];
//    nameL.font = [UIFont boldSystemFontOfSize:12];
//    [imgV addSubview:nameL];
//    _nameL = nameL;
    
    UILabel *shopType = [[UILabel alloc] init];
    if ([self.user.joinin_state isEqualToString:@"20"]) {
        shopType.text = @"状态 : 审核通过";
    }else{
        shopType.text = @"状态 : 正在审核";
    }
    shopType.textColor = [UIColor whiteColor];
    shopType.font = [UIFont boldSystemFontOfSize:12];
    [imgV addSubview:shopType];
    _shopType = shopType;
    
    UIView *btnBGView = [[UIView alloc] init];
    btnBGView.backgroundColor = WYColor(249, 175, 109);
    [self addSubview:btnBGView];
    
    MineButton *mineLBtn = [[MineButton alloc] initWithFrame:CGRectMake(0, 0, kWidth/3, 50)];
    mineLBtn.titleL.text = @"店铺总额度";
    [mineLBtn addTarget:self action:@selector(headerClcik:) forControlEvents:UIControlEventTouchUpInside];
    mineLBtn.tag = MBtnTotal;
    [btnBGView addSubview:mineLBtn];
    _mineLBtn = mineLBtn;
    
    MineButton *mineCBtn = [[MineButton alloc] initWithFrame:CGRectMake(0, 0, kWidth/3, 50)];
    mineCBtn.titleL.text = @"已分配劵额度";
    [mineCBtn addTarget:self action:@selector(headerClcik:) forControlEvents:UIControlEventTouchUpInside];
    mineCBtn.tag = MBtnAlready;
    [btnBGView addSubview:mineCBtn];
    _mineCBtn = mineCBtn;
    
    MineButton *mineRBtn = [[MineButton alloc] initWithFrame:CGRectMake(0, 0, kWidth/3, 50)];
    mineRBtn.titleL.text = @"已核销额度";
    [mineRBtn addTarget:self action:@selector(headerClcik:) forControlEvents:UIControlEventTouchUpInside];
    mineRBtn.tag = MBtnLine;
    [btnBGView addSubview:mineRBtn];
    _mineRBtn = mineRBtn;

    if (![self.user.joinin_state isEqualToString:@"20"]) {
        _mineLBtn.numL.text = @"0";
        _mineCBtn.numL.text = @"0";
        _mineRBtn.numL.text = @"0";
    }else{
        _mineLBtn.numL.text = self.user.credit_total;
        _mineCBtn.numL.text = self.user.credit_coupon_assigned;
        _mineRBtn.numL.text = self.user.credit_coupon_consumed;
    }

    
    
    __weak __typeof(&*self)weakSelf = self;
    [imgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(weakSelf);
        make.height.equalTo(@150);
    }];
    [avatorImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(imgV.mas_centerY).offset(20);
        make.left.equalTo(weakSelf.mas_left).offset(30);
        make.size.mas_equalTo(avatorImg.frame.size);
    }];
    [shopNameL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(avatorImg.mas_top).offset(10);
        make.left.equalTo(avatorImg.mas_right).offset(20);
    }];
//    [nameL mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(shopNameL.mas_bottom).offset(8);
//        make.left.equalTo(shopNameL.mas_left);
//    }];
    [shopType mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerY.mas_equalTo(nameL);
        make.left.equalTo(shopNameL.mas_left);
        make.top.equalTo(shopNameL.mas_bottom).offset(8);
    }];
    [btnBGView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(weakSelf);
        make.top.equalTo(imgV.mas_bottom);
    }];
    NSMutableArray *btnArr = [NSMutableArray array];
    [btnArr addObjectsFromArray:@[mineLBtn,mineCBtn,mineRBtn]];
    [HWCommon makeEqualWidthViews:btnArr inView:btnBGView LRpadding:0 viewPadding:0];
}

- (void)headerClcik:(UIButton *)btn{
    
    
}


- (SCUser *)user{
    if (_user == nil) {
        _user = [SCUserTool loadInfo];
    }
    return _user;
}


@end
